### REPLICATION 
### Business Owners and Executives as Politicians: The Effect on Public Policy
### this script cleans results for plots & tables


## main results - non-RDrobust w/ CCT optimal bandwidths
load("replication_CCT_bandwidth_results_constant_dollars.RData")
## results - multi-year analysis
load("replication_CCT_bandwidth_multi_year_results_constant_dollars.RData")
## results - party analysis
load("replication_CCT_party_results_constant_dollars.RData")

## supplemental results
## results - covariate continuity & placebo tests
load("replication_results_covs_balance_tests.RData")
load("replication_results_covs_balance_tests_rdrobust.RData")
## results - quadratic RD specs
load("replication_results_CCT_quadratic_specs.RData")
## results - spending shares - non-RDrobust w/ CCT optimal bandwidths
load("replication_CCT_bandwidth_results_spending_shares.RData")
## results - non-RDrobust multiple bandwidths
load("replication_multi_bandwidth_results_constant_dollars.RData")
## results - RDrobust
load("replication_RD_results_rdrobust.RData")
## results - form of govt analysis
load("replication_fog_CCT_results.Rdata")


#### Main Results
gg_results_CCT <-
  results_CCT %>% 
  mutate(outcome_class = case_when(grepl("ttl_", outcome) | 
                                     grepl("debt", outcome) ~ "size",
                                   grepl("basic", outcome) | 
                                     grepl("redistributive", outcome) | 
                                     grepl("developmental", outcome) ~ "spending category",
                                   TRUE ~ "spending"),
         outcome_type = case_when(grepl("revs", outcome) | 
                                    grepl("chgs", outcome) |
                                    grepl("taxes", outcome) ~ "revenue",
                                  grepl("basic", outcome) ~ "basic services spending", 
                                  grepl("redistributive", outcome) ~ "redistributive spending", 
                                  grepl("developmental", outcome) ~ "developmental spending",
                                  grepl("debt", outcome) ~ "debt",
                                  grepl("exps", outcome) ~ "expenditure",
                                  outcome == "police" |
                                    outcome == "fire" |
                                    outcome == "admin" |
                                    outcome == "waste" ~ "basic services spending",
                                  outcome == "roads" |
                                    outcome == "parks" |
                                    outcome == "libraries" ~ "developmental spending",
                                  outcome == "health" |
                                    outcome == "housing" |
                                    outcome == "welfare" ~ "redistributive spending"),
         coef = as.numeric(as.character(coef)),
         robust_se = as.numeric(as.character(robust_se)),
         outcome_type = factor(outcome_type, levels = c("revenue", "debt", "expenditure", 
                                                        "basic services spending", "developmental spending", "redistributive spending"),
                               labels = c("Revenue", "Debt", "Expenditure",
                                          "Basic \n Services", "Infrastructure & \n Amenities", "Redistributive \n Programs")),
         outcome = factor(outcome, levels = c("redistributive","welfare", "housing", "health", 
                                              "developmental","roads", "libraries", "parks",  
                                              "basic_services", "admin", "waste", "fire", "police", 
                                              "ttl_revs", "ttl_own_revs", "ttl_taxes", "ttl_sales_taxes", "ttl_prop_taxes", "ttl_chgs_miscrev",
                                              "ttl_debt", "ttl_debt_issued", "st_debt", "ttl_exps"), 
                          labels = c("Total Redistributive \n Programs", "Welfare", "Housing & Community \nDevelopment", "Health", 
                                     "Total Infrastructure & \n Amenities", "Roads", "Libraries", "Parks", 
                                     "Total Basic \n Services", "Administration", "Sanitation", "Fire", "Police", 
                                     "Total Revenue", "Own-source \n Revenue", "Total Taxes","Sales Tax", "Property Tax", "Charges & Misc. \n Revenue",  
                                     "Total Debt", "Total Debt Issued", "Short-term Debt", "Total Expenditure")))


####  Multi-year results
gg_multi_year_results_CCT <-
  results_CCT_multi_year %>% 
  filter(lead_years != "_lead5", lead_years != "_lead6") %>% 
  mutate(outcome_class = case_when(grepl("ttl_", outcome) | 
                                     grepl("debt", outcome) ~ "size",
                                   grepl("basic", outcome) | 
                                     grepl("redistributive", outcome) | 
                                     grepl("developmental", outcome) ~ "spending category",
                                   TRUE ~ "spending"),
         outcome_type = case_when(grepl("revs", outcome) | 
                                    grepl("chgs", outcome) |
                                    grepl("taxes", outcome) ~ "revenue",
                                  grepl("basic", outcome) ~ "basic services spending", 
                                  grepl("redistributive", outcome) ~ "redistributive spending", 
                                  grepl("developmental", outcome) ~ "developmental spending",
                                  grepl("debt", outcome) ~ "debt",
                                  grepl("exps", outcome) ~ "expenditure",
                                  outcome == "police" |
                                    outcome == "fire" |
                                    outcome == "admin" |
                                    outcome == "waste" ~ "basic services spending",
                                  outcome == "roads" |
                                    outcome == "parks" |
                                    outcome == "libraries" ~ "developmental spending",
                                  outcome == "health" |
                                    outcome == "housing" |
                                    outcome == "welfare" ~ "redistributive spending"),
         coef = as.numeric(as.character(coef)),
         robust_se = as.numeric(as.character(robust_se)),
         outcome_type = factor(outcome_type, levels = c("revenue", "debt", "expenditure", 
                                                        "basic services spending", "developmental spending", "redistributive spending"),
                               labels = c("Revenue", "Debt", "Expenditure",
                                          "Basic \n Services", "Infrastructure & \n Amenities", "Redistributive \n Programs")),
         outcome = factor(outcome, levels = c("redistributive","welfare", "housing", "health", 
                                              "developmental","roads", "libraries", "parks",  
                                              "basic_services", "admin", "waste", "fire", "police", 
                                              "ttl_revs", "ttl_own_revs", "ttl_taxes", "ttl_sales_taxes", "ttl_prop_taxes", "ttl_chgs_miscrev",
                                              "ttl_debt", "ttl_debt_issued", "st_debt", "ttl_exps"), 
                          labels = c("Total Redistributive \n Programs", "Welfare", "Housing & Community \nDevelopment", "Health", 
                                     "Total Infrastructure & \n Amenities", "Roads", "Libraries", "Parks", 
                                     "Total Basic \n Services", "Administration", "Sanitation", "Fire", "Police", 
                                     "Total Revenue", "Own-source \n Revenue", "Total Taxes","Sales Tax", "Property Tax", "Charges & Misc. \n Revenue",  
                                     "Total Debt", "Total Debt Issued", "Short-term Debt", "Total Expenditure")))



#### Party analysis
tab_alt_specs_wide <-
  party_results_CCT %>% 
  mutate(covs = case_when(grepl("covs", specs) ~ "covs",
                          TRUE ~ "base"),
         dv_form = case_when(grepl("log", specs) ~ "log",
                             grepl("diff", specs) ~ "dollars diff",
                             TRUE ~ "dollars")) %>% 
  unite("outcome_specs", outcome, dv_form, candidate_type, sep = "|") %>% 
  select(-specs) %>% 
  gather(statistic, value, -outcome_specs, -covs) %>% 
  unite("statistic", covs, statistic, sep = "_") %>% 
  separate(outcome_specs, c("outcome", "specs", "candidate_type"), sep = "([|])") %>% 
  spread(statistic, value)


tab_alt_specs <- 
  tab_alt_specs_wide %>% 
  mutate(outcome_class = case_when(grepl("ttl_", outcome) | 
                                     grepl("debt", outcome) ~ "size",
                                   grepl("basic", outcome) | 
                                     grepl("redistributive", outcome) | 
                                     grepl("developmental", outcome) ~ "spending category",
                                   TRUE ~ "spending"),
         outcome_type = case_when(grepl("revs", outcome) | 
                                    grepl("chgs", outcome) |
                                    grepl("taxes", outcome) ~ "revenue",
                                  grepl("basic", outcome) ~ "basic services spending", 
                                  grepl("redistributive", outcome) ~ "redistributive spending", 
                                  grepl("developmental", outcome) ~ "developmental spending",
                                  grepl("debt", outcome) ~ "debt",
                                  grepl("exps", outcome) ~ "expenditure",
                                  outcome == "police" |
                                    outcome == "fire" |
                                    outcome == "admin" |
                                    outcome == "waste" ~ "basic services spending",
                                  outcome == "roads" |
                                    outcome == "parks" |
                                    outcome == "libraries" ~ "developmental spending",
                                  outcome == "health" |
                                    outcome == "housing" |
                                    outcome == "welfare" ~ "redistributive spending"),
         outcome_type = factor(outcome_type, levels = c("revenue", "debt", "expenditure", 
                                                        "basic services spending", "developmental spending", "redistributive spending"),
                               labels = c("Revenue", "Debt", "Expenditure",
                                          "Basic \n Services", "Infrastructure & \n Amenities", "Redistributive \n Programs")),
         outcome = factor(outcome, levels = c("redistributive","welfare", "housing", "health", 
                                              "developmental","roads", "libraries", "parks",  
                                              "basic_services", "admin", "waste", "fire", "police", 
                                              "ttl_revs", "ttl_own_revs", "ttl_taxes", "ttl_sales_taxes", "ttl_prop_taxes", "ttl_chgs_miscrev",
                                              "ttl_debt", "ttl_debt_issued", "st_debt", "ttl_exps"), 
                          labels = c("Redistributive \n Programs", "Welfare", "Housing & Comm. \nDevel't.", "Health", 
                                     "Infrastructure & \n Amenities", "Roads", "Libraries", "Parks", 
                                     "Basic \n Services", "Administration", "Sanitation", "Fire", "Police", 
                                     "Total Revenue", "Own-source \n Revenue", "Total Taxes","Sales Tax", "Property Tax", "Charges & Misc. \n Revenue",  
                                     "Total Debt", "Total Debt Issued", "Short-term Debt", "Total Expenditure")),
         dv_form = case_when(grepl("log", specs)  ~ "Dollars (per capita, logged)",
                             specs == "dollars"  ~ "Dollars (per capita)",
                             grepl("diff", specs) ~ "Diffference in Dollars (per capita)"),
         dv_form = factor(dv_form, levels = c("Dollars (per capita)", "Dollars (per capita, logged)", "Diffference in Dollars (per capita)")),
         base_coef = as.numeric(as.character(base_coef)),
         base_robust_se = as.numeric(as.character(base_robust_se)),
         base_robust_pval = as.numeric(as.character(base_robust_pval)),
         base_h = as.numeric(as.character(base_h)),
         base_n = as.numeric(as.character(base_n)),
         covs_coef = as.numeric(as.character(covs_coef)),
         covs_robust_se = as.numeric(as.character(covs_robust_se)),
         covs_robust_pval = as.numeric(as.character(covs_robust_pval)),
         covs_h = as.numeric(as.character(covs_h)),
         covs_n = as.numeric(as.character(covs_n)))



gg_republican <- 
  tab_alt_specs %>% 
  filter(specs == "dollars" & (candidate_type == "partisan_rep" | candidate_type == "one_rep")) %>% 
  mutate(candidate_type = case_when(candidate_type == "partisan_rep" ~ "Republican vs. Democrat",
                                    candidate_type == "one_rep" ~ "Republican vs. Non-Republican"))


tab_alt_specs_size_party <- 
  tab_alt_specs %>% 
  filter(outcome_class == "size") %>% 
  arrange(outcome_type, outcome, dv_form)

tab_alt_specs_spending_party <- 
  tab_alt_specs %>% 
  filter(outcome_class == "spending") %>% 
  arrange(outcome_type, outcome, dv_form)

tab_alt_specs_spending_category_party <- 
  tab_alt_specs %>% 
  filter(outcome_class == "spending category") %>% 
  arrange(outcome_type, outcome, dv_form)



#### Covariate continuity & placebo tests
## appendix - Table A.1 - placebo tests (all - non-RDrobust)
results_balance_covs_CCT_wide <- rename_all(results_balance_covs_CCT, paste0, "_CCT")
results_balance_covs_05_wide <- rename_all(results_balance_covs_05, paste0, "_05")
results_balance_all_wide <- cbind(results_balance_covs_CCT_wide, results_balance_covs_05_wide)


## table formatting
results_balance_covs_CCT$robust_se <- paste0("(", results_balance_covs_CCT$robust_se, ")")
results_balance_covs_05$robust_se <- paste0("(", results_balance_covs_05$robust_se, ")")
results_balance_all_wide$robust_se_CCT <- paste0("(", results_balance_all_wide$robust_se_CCT, ")")
results_balance_all_wide$robust_se_05 <- paste0("(", results_balance_all_wide$robust_se_05, ")")


row.names(results_balance_all_wide) <- c("Total Revenue (per-capita, lagged)", 
                                         "Total Expenditures (per-capita, lagged)", 
                                         "Own-source Revenue (per-capita, lagged)", 
                                         "Total Taxes (per-capita, lagged)", 
                                         "Sales Taxes (per-capita, lagged)",
                                         "Property Taxes (per-capita, lagged)", 
                                         "Charges & Misc. Revenue (per-capita, lagged)", 
                                         "Total Debt (per-capita, lagged)", 
                                         "Debt Issued (per-capita, lagged)", 
                                         "Short-term Debt (per-capita, lagged)", 
                                         "Police (per-capita, lagged)", 
                                         "Fire (per-capita, lagged)", 
                                         "Administration (per-capita, lagged)", 
                                         "Sanitation (per-capita, lagged)", 
                                         "Roads (per-capita, lagged)", 
                                         "Parks (per-capita, lagged)", 
                                         "Libraries (per-capita, lagged)", 
                                         "Health (per-capita, lagged)", 
                                         "Housing & Comm. Development (per-capita, lagged)", 
                                         "Welfare (per-capita, lagged)", 
                                         "Population (log)", 
                                         "Percent White", 
                                         "Unemployment Rate", 
                                         "Home-ownership Rate",
                                         "Median Household Income",
                                         "Median House Value (log)",
                                         "Municipal Form")


row.names(results_balance_rdrobust_covs) <- c("Total Revenue (per-capita, lagged)", 
                                              "Total Expenditures (per-capita, lagged)", 
                                              "Own-source Revenue (per-capita, lagged)", 
                                              "Total Taxes (per-capita, lagged)", 
                                              "Sales Taxes (per-capita, lagged)",
                                              "Property Taxes (per-capita, lagged)", 
                                              "Charges & Misc. Revenue (per-capita, lagged)", 
                                              "Total Debt (per-capita, lagged)", 
                                              "Debt Issued (per-capita, lagged)", 
                                              "Short-term Debt (per-capita, lagged)", 
                                              "Police (per-capita, lagged)", 
                                              "Fire (per-capita, lagged)", 
                                              "Administration (per-capita, lagged)", 
                                              "Sanitation (per-capita, lagged)", 
                                              "Roads (per-capita, lagged)", 
                                              "Parks (per-capita, lagged)", 
                                              "Libraries (per-capita, lagged)", 
                                              "Health (per-capita, lagged)", 
                                              "Housing & Comm. Development (per-capita, lagged)", 
                                              "Welfare (per-capita, lagged)", 
                                              "Population (log)", 
                                              "Percent White", 
                                              "Unemployment Rate", 
                                              "Home-ownership Rate",
                                              "Median Household Income",
                                              "Median House Value (log)",
                                              "Municipal Form")


results_balance_rdrobust_covs <- within(results_balance_rdrobust_covs, {
  ci <- NA    
  ci <- paste0("[", ci_low, ", ", ci_upp, "]")
  
})


#### Quadratic RD specifications 
tab_alt_specs_quad <- 
  results_CCT_quadratic %>% 
  mutate(outcome_class = case_when(grepl("ttl_", outcome) | 
                                     grepl("debt", outcome) ~ "size",
                                   grepl("basic", outcome) | 
                                     grepl("redistributive", outcome) | 
                                     grepl("developmental", outcome) ~ "spending category",
                                   TRUE ~ "spending"),
         outcome_type = case_when(grepl("revs", outcome) | 
                                    grepl("chgs", outcome) |
                                    grepl("taxes", outcome) ~ "revenue",
                                  grepl("basic", outcome) ~ "basic services spending", 
                                  grepl("redistributive", outcome) ~ "redistributive spending", 
                                  grepl("developmental", outcome) ~ "developmental spending",
                                  grepl("debt", outcome) ~ "debt",
                                  grepl("exps", outcome) ~ "expenditure",
                                  outcome == "police" |
                                    outcome == "fire" |
                                    outcome == "admin" |
                                    outcome == "waste" ~ "basic services spending",
                                  outcome == "roads" |
                                    outcome == "parks" |
                                    outcome == "libraries" ~ "developmental spending",
                                  outcome == "health" |
                                    outcome == "housing" |
                                    outcome == "welfare" ~ "redistributive spending"),
         outcome_type = factor(outcome_type, levels = c("revenue", "debt", "expenditure", 
                                                        "basic services spending", "developmental spending", "redistributive spending"),
                               labels = c("Revenue", "Debt", "Expenditure",
                                          "Basic \n Services", "Infrastructure & \n Amenities", "Redistributive \n Programs")),
         outcome = factor(outcome, levels = c("redistributive","welfare", "housing", "health", 
                                              "developmental","roads", "libraries", "parks",  
                                              "basic_services", "admin", "waste", "fire", "police", 
                                              "ttl_revs", "ttl_own_revs", "ttl_taxes", "ttl_sales_taxes", "ttl_prop_taxes", "ttl_chgs_miscrev",
                                              "ttl_debt", "ttl_debt_issued", "st_debt", "ttl_exps"), 
                          labels = c("Redistributive \n Programs", "Welfare", "Housing & Comm. \nDevel't.", "Health", 
                                     "Infrastructure & \n Amenities", "Roads", "Libraries", "Parks", 
                                     "Basic \n Services", "Administration", "Sanitation", "Fire", "Police", 
                                     "Total Revenue", "Own-source \n Revenue", "Total Taxes","Sales Tax", "Property Tax", "Charges & Misc. \n Revenue",  
                                     "Total Debt", "Total Debt Issued", "Short-term Debt", "Total Expenditure")),
         coef = as.numeric(as.character(coef)),
         robust_se = as.numeric(as.character(robust_se)),
         robust_pval = as.numeric(as.character(robust_pval)),
         h = as.numeric(as.character(h)),
         n = as.numeric(as.character(n)))


## Alternative specifications (Supplemental Information)
tab_alt_specs_wide <-
  results_CCT %>% 
  mutate(covs = case_when(grepl("covs", specs) ~ "covs",
                          TRUE ~ "base"),
         dv_form = case_when(grepl("log", specs) ~ "log",
                             grepl("diff", specs) ~ "dollars diff",
                             TRUE ~ "dollars")) %>% 
  unite("outcome_specs", outcome, dv_form, sep = "|") %>% 
  select(-specs) %>% 
  gather(statistic, value, -outcome_specs, -covs) %>% 
  unite("statistic", covs, statistic, sep = "_") %>% 
  separate(outcome_specs, c("outcome", "specs"), sep = "([|])") %>% 
  spread(statistic, value)


tab_alt_specs <- 
  tab_alt_specs_wide %>% 
  mutate(outcome_class = case_when(grepl("ttl_", outcome) | 
                                     grepl("debt", outcome) ~ "size",
                                   grepl("basic", outcome) | 
                                     grepl("redistributive", outcome) | 
                                     grepl("developmental", outcome) ~ "spending category",
                                   TRUE ~ "spending"),
         outcome_type = case_when(grepl("revs", outcome) | 
                                    grepl("chgs", outcome) |
                                    grepl("taxes", outcome) ~ "revenue",
                                  grepl("basic", outcome) ~ "basic services spending", 
                                  grepl("redistributive", outcome) ~ "redistributive spending", 
                                  grepl("developmental", outcome) ~ "developmental spending",
                                  grepl("debt", outcome) ~ "debt",
                                  grepl("exps", outcome) ~ "expenditure",
                                  outcome == "police" |
                                    outcome == "fire" |
                                    outcome == "admin" |
                                    outcome == "waste" ~ "basic services spending",
                                  outcome == "roads" |
                                    outcome == "parks" |
                                    outcome == "libraries" ~ "developmental spending",
                                  outcome == "health" |
                                    outcome == "housing" |
                                    outcome == "welfare" ~ "redistributive spending"),
         outcome_type = factor(outcome_type, levels = c("revenue", "debt", "expenditure", 
                                                        "basic services spending", "developmental spending", "redistributive spending"),
                               labels = c("Revenue", "Debt", "Expenditure",
                                          "Basic \n Services", "Infrastructure & \n Amenities", "Redistributive \n Programs")),
         outcome = factor(outcome, levels = c("redistributive","welfare", "housing", "health", 
                                              "developmental","roads", "libraries", "parks",  
                                              "basic_services", "admin", "waste", "fire", "police", 
                                              "ttl_revs", "ttl_own_revs", "ttl_taxes", "ttl_sales_taxes", "ttl_prop_taxes", "ttl_chgs_miscrev",
                                              "ttl_debt", "ttl_debt_issued", "st_debt", "ttl_exps"), 
                          labels = c("Redistributive \n Programs", "Welfare", "Housing & Comm. \nDevel't.", "Health", 
                                     "Infrastructure & \n Amenities", "Roads", "Libraries", "Parks", 
                                     "Basic \n Services", "Administration", "Sanitation", "Fire", "Police", 
                                     "Total Revenue", "Own-source \n Revenue", "Total Taxes","Sales Tax", "Property Tax", "Charges & Misc. \n Revenue",  
                                     "Total Debt", "Total Debt Issued", "Short-term Debt", "Total Expenditure")),
         dv_form = case_when(grepl("log", specs)  ~ "Dollars (per capita, logged)",
                             specs == "dollars"  ~ "Dollars (per capita)",
                             grepl("diff", specs) ~ "Diffference in Dollars (per capita)"),
         dv_form = factor(dv_form, levels = c("Dollars (per capita)", "Dollars (per capita, logged)", "Diffference in Dollars (per capita)")),
         base_coef = as.numeric(as.character(base_coef)),
         base_robust_se = as.numeric(as.character(base_robust_se)),
         base_robust_pval = as.numeric(as.character(base_robust_pval)),
         base_h = as.numeric(as.character(base_h)),
         base_n = as.numeric(as.character(base_n)),
         covs_coef = as.numeric(as.character(covs_coef)),
         covs_robust_se = as.numeric(as.character(covs_robust_se)),
         covs_robust_pval = as.numeric(as.character(covs_robust_pval)),
         covs_h = as.numeric(as.character(covs_h)),
         covs_n = as.numeric(as.character(covs_n)))

tab_alt_specs_size <- 
  tab_alt_specs %>% 
  filter(outcome_class == "size") %>% 
  arrange(outcome_type, outcome, dv_form)

tab_alt_specs_spending <- 
  tab_alt_specs %>% 
  filter(outcome_class == "spending") %>% 
  arrange(outcome_type, outcome, dv_form)

tab_alt_specs_spending_category <- 
  tab_alt_specs %>% 
  filter(outcome_class == "spending category") %>% 
  arrange(outcome_type, outcome, dv_form)

tab_alt_specs_debt <- 
  tab_alt_specs %>% 
  filter(outcome == "Total Debt") %>% 
  arrange(dv_form)



##################################################
#### muti-bandwidth/multi-specification plots ####
##################################################

gg_multi_bandwidth <-
  results_dollars %>% 
  mutate(dv_form = case_when(specs == "log" | specs == "log covs"  ~ "Dollars (per capita, logged)",
                             specs == "base" | specs == "covs"  ~ "Dollars (per capita)",
                             grepl("diff", specs) ~ "Diffference in Dollars (per capita)"),
         dv_type = case_when(specs == "base" | specs == "log" | specs == "diff" ~ "Base Model",
                             specs == "covs" | specs == "log covs" | specs == "diff covs" ~ "Covariate-adjusted Model"),
         dv_type = factor(dv_type, levels = c("Base Model", "Covariate-adjusted Model")),
         coef = as.numeric(as.character(coef)),
         robust_se = as.numeric(as.character(robust_se)),
         h = as.numeric(as.character(h)))

gg_rdrobust <-
  results_rdrobust %>% 
  mutate(dv_form = case_when(specs == "log" | specs == "log covs"  ~ "Dollars (per capita, logged)",
                             specs == "base" | specs == "covs" ~ "Dollars (per capita)",
                             grepl("diff", specs) ~ "Diffference in Dollars (per capita)"),
         dv_type = case_when(specs == "base" | specs == "log" | specs == "differenced dv" ~ "Base Model",
                             specs == "covs" | specs == "log covs" | specs == "differenced covs" ~ "Covariate-adjusted Model"),
         dv_type = factor(dv_type, levels = c("Base Model", "Covariate-adjusted Model")),
         coef = as.numeric(as.character(coef)),
         ci_low = as.numeric(as.character(ci_low)),
         ci_upp = as.numeric(as.character(ci_upp)),
         h = as.numeric(as.character(h)))




###################################
#### results - spending shares ####
###################################

tab_alt_specs_spending_shares <-
  results_spending_shares_CCT %>% 
  mutate(covs = case_when(grepl("covs", specs) ~ "covs",
                          TRUE ~ "base"),
         dv_form = case_when(grepl("log", specs) ~ "log",
                             grepl("diff", specs) ~ "dollars diff",
                             TRUE ~ "dollars")) %>% 
  unite("outcome_specs", outcome, dv_form, sep = "|") %>% 
  select(-specs) %>% 
  gather(statistic, value, -outcome_specs, -covs) %>% 
  unite("statistic", covs, statistic, sep = "_") %>% 
  separate(outcome_specs, c("outcome", "specs"), sep = "([|])") %>% 
  spread(statistic, value) %>% 
  mutate(outcome_type = case_when(grepl("basic", outcome) ~ "basic services spending", 
                                  grepl("redistributive", outcome) ~ "redistributive spending", 
                                  grepl("developmental", outcome) ~ "developmental spending",
                                  outcome == "police" |
                                    outcome == "fire" |
                                    outcome == "admin" |
                                    outcome == "waste" ~ "basic services spending",
                                  outcome == "roads" |
                                    outcome == "parks" |
                                    outcome == "libraries" ~ "developmental spending",
                                  outcome == "health" |
                                    outcome == "housing" |
                                    outcome == "welfare" ~ "redistributive spending"),
         outcome_type = factor(outcome_type, levels = c("basic services spending", "developmental spending", "redistributive spending"),
                               labels = c("Basic \n Services", "Infrastructure & \n Amenities", "Redistributive \n Programs")),
         outcome = factor(outcome, levels = c("redistributive","welfare", "housing", "health", 
                                              "developmental","roads", "libraries", "parks",  
                                              "basic_services", "admin", "waste", "fire", "police"), 
                          labels = c("Redistributive \n Programs", "Welfare", "Housing & Comm. \nDevel't.", "Health", 
                                     "Infrastructure & \n Amenities", "Roads", "Libraries", "Parks", 
                                     "Basic \n Services", "Administration", "Sanitation", "Fire", "Police")),
         dv_form = case_when(grepl("log", specs)  ~ "Dollars (per capita, logged)",
                             specs == "dollars"  ~ "Dollars (per capita)",
                             grepl("diff", specs) ~ "Diffference in Dollars (per capita)"),
         dv_form = factor(dv_form, levels = c("Dollars (per capita)", "Dollars (per capita, logged)", "Diffference in Dollars (per capita)")),
         base_coef = as.numeric(as.character(base_coef)),
         base_robust_se = as.numeric(as.character(base_robust_se)),
         base_robust_pval = as.numeric(as.character(base_robust_pval)),
         base_h = as.numeric(as.character(base_h)),
         base_n = as.numeric(as.character(base_n)),
         covs_coef = as.numeric(as.character(covs_coef)),
         covs_robust_se = as.numeric(as.character(covs_robust_se)),
         covs_robust_pval = as.numeric(as.character(covs_robust_pval)),
         covs_h = as.numeric(as.character(covs_h)),
         covs_n = as.numeric(as.character(covs_n))) %>%
  arrange(outcome_type, outcome, dv_form)



###############################
#### results with RDrobust ####
###############################

main_results_rdrobust <-
  results_rdrobust %>% 
  mutate(outcome_class = case_when(grepl("ttl_", outcome) | 
                                     grepl("debt", outcome) ~ "size",
                                   grepl("basic", outcome) | 
                                     grepl("redistributive", outcome) | 
                                     grepl("developmental", outcome) ~ "spending category",
                                   TRUE ~ "spending"),
         outcome_type = case_when(grepl("revs", outcome) | 
                                    grepl("chgs", outcome) |
                                    grepl("taxes", outcome) ~ "revenue",
                                  grepl("basic", outcome) ~ "basic services spending", 
                                  grepl("redistributive", outcome) ~ "redistributive spending", 
                                  grepl("developmental", outcome) ~ "developmental spending",
                                  grepl("debt", outcome) ~ "debt",
                                  grepl("exps", outcome) ~ "expenditure",
                                  outcome == "police" |
                                    outcome == "fire" |
                                    outcome == "admin" |
                                    outcome == "waste" ~ "basic services spending",
                                  outcome == "roads" |
                                    outcome == "parks" |
                                    outcome == "libraries" ~ "developmental spending",
                                  outcome == "health" |
                                    outcome == "housing" |
                                    outcome == "welfare" ~ "redistributive spending"),
         coef = as.numeric(as.character(coef)),
         ci = paste0("[", ci_low, ", ", ci_upp, "]"),
         outcome_type = factor(outcome_type, levels = c("revenue", "debt", "expenditure", 
                                                        "basic services spending", "developmental spending", "redistributive spending"),
                               labels = c("Revenue", "Debt", "Expenditure",
                                          "Basic \n Services", "Infrastructure & \n Amenities", "Redistributive \n Programs")),
         outcome = factor(outcome, levels = c("redistributive","welfare", "housing", "health", 
                                              "developmental","roads", "libraries", "parks",  
                                              "basic_services", "admin", "waste", "fire", "police", 
                                              "ttl_revs", "ttl_own_revs", "ttl_taxes", "ttl_sales_taxes", "ttl_prop_taxes", "ttl_chgs_miscrev",
                                              "ttl_debt", "ttl_debt_issued", "st_debt", "ttl_exps"), 
                          labels = c("Total Redistributive \n Programs", "Welfare", "Housing & Community \nDevelopment", "Health", 
                                     "Total Infrastructure & \n Amenities", "Roads", "Libraries", "Parks", 
                                     "Total Basic \n Services", "Administration", "Sanitation", "Fire", "Police", 
                                     "Total Revenue", "Own-source \n Revenue", "Total Taxes","Sales Tax", "Property Tax", "Charges & Misc. \n Revenue",  
                                     "Total Debt", "Total Debt Issued", "Short-term Debt", "Total Expenditure")))



#####################################
####    Form of Govt Analyses    ####
#####################################


tab_alt_specs_wide_FOG <-
  fog_results_CCT %>% 
  mutate(covs = case_when(grepl("covs", specs) ~ "covs",
                          TRUE ~ "base"),
         dv_form = case_when(grepl("log", specs) ~ "log",
                             grepl("diff", specs) ~ "dollars diff",
                             TRUE ~ "dollars")) %>% 
  unite("outcome_specs", outcome, dv_form, form_govt, sep = "|") %>% 
  select(-specs) %>% 
  gather(statistic, value, -outcome_specs, -covs) %>% 
  unite("statistic", covs, statistic, sep = "_") %>% 
  separate(outcome_specs, c("outcome", "specs", "form_govt"), sep = "([|])") %>% 
  spread(statistic, value)


tab_alt_specs_FOG <- 
  tab_alt_specs_wide_FOG %>% 
  mutate(outcome_class = case_when(grepl("ttl_", outcome) | 
                                     grepl("debt", outcome) ~ "size",
                                   grepl("basic", outcome) | 
                                     grepl("redistributive", outcome) | 
                                     grepl("developmental", outcome) ~ "spending category",
                                   TRUE ~ "spending"),
         outcome_type = case_when(grepl("revs", outcome) | 
                                    grepl("chgs", outcome) |
                                    grepl("taxes", outcome) ~ "revenue",
                                  grepl("basic", outcome) ~ "basic services spending", 
                                  grepl("redistributive", outcome) ~ "redistributive spending", 
                                  grepl("developmental", outcome) ~ "developmental spending",
                                  grepl("debt", outcome) ~ "debt",
                                  grepl("exps", outcome) ~ "expenditure",
                                  outcome == "police" |
                                    outcome == "fire" |
                                    outcome == "admin" |
                                    outcome == "waste" ~ "basic services spending",
                                  outcome == "roads" |
                                    outcome == "parks" |
                                    outcome == "libraries" ~ "developmental spending",
                                  outcome == "health" |
                                    outcome == "housing" |
                                    outcome == "welfare" ~ "redistributive spending"),
         outcome_type = factor(outcome_type, levels = c("revenue", "debt", "expenditure", 
                                                        "basic services spending", "developmental spending", "redistributive spending"),
                               labels = c("Revenue", "Debt", "Expenditure",
                                          "Basic \n Services", "Infrastructure & \n Amenities", "Redistributive \n Programs")),
         outcome = factor(outcome, levels = c("redistributive","welfare", "housing", "health", 
                                              "developmental","roads", "libraries", "parks",  
                                              "basic_services", "admin", "waste", "fire", "police", 
                                              "ttl_revs", "ttl_own_revs", "ttl_taxes", "ttl_sales_taxes", "ttl_prop_taxes", "ttl_chgs_miscrev",
                                              "ttl_debt", "ttl_debt_issued", "st_debt", "ttl_exps"), 
                          labels = c("Redistributive \n Programs", "Welfare", "Housing & Comm. \nDevel't.", "Health", 
                                     "Infrastructure & \n Amenities", "Roads", "Libraries", "Parks", 
                                     "Basic \n Services", "Administration", "Sanitation", "Fire", "Police", 
                                     "Total Revenue", "Own-source \n Revenue", "Total Taxes","Sales Tax", "Property Tax", "Charges & Misc. \n Revenue",  
                                     "Total Debt", "Total Debt Issued", "Short-term Debt", "Total Expenditure")),
         dv_form = case_when(grepl("log", specs)  ~ "Dollars (per capita, logged)",
                             specs == "dollars"  ~ "Dollars (per capita)",
                             grepl("diff", specs) ~ "Diffference in Dollars (per capita)"),
         dv_form = factor(dv_form, levels = c("Dollars (per capita)", "Dollars (per capita, logged)", "Diffference in Dollars (per capita)")),
         covs_coef = as.numeric(as.character(covs_coef)),
         covs_robust_se = as.numeric(as.character(covs_robust_se)),
         covs_robust_pval = as.numeric(as.character(covs_robust_pval)),
         covs_h = as.numeric(as.character(covs_h)),
         covs_n = as.numeric(as.character(covs_n)),
         form_govt = factor(form_govt, levels = c("council_manager", "mayor_council", "non_mayor_council"),
                            labels = c("Council-Manager", "Mayor-Council", "Non-Mayor-Council")))

tab_alt_specs_size_FOG <- 
  tab_alt_specs_FOG %>% 
  filter(outcome_class == "size", specs == "dollars") %>% 
  arrange(outcome_type, outcome, dv_form)

tab_alt_specs_spending_FOG <- 
  tab_alt_specs_FOG %>% 
  filter(outcome_class == "spending", specs == "dollars") %>% 
  arrange(outcome_type, outcome, dv_form)



